//
//  BMWDragButton.m
//  XYuanTiKu
//
//  Created by erice on 15/10/28.
//  Copyright © 2015年 XiongYing Tec. All rights reserved.
//

#import "BMWDragButton.h"
#import "NTalkerChatViewController.h"


static void *AdsorbEnableKey = &AdsorbEnableKey;
@implementation BMWDragButton

- (void)dealloc
{
    NSLog(@"BMWDragButton dealloc"); 
    [super dealloc];
}

#pragma mark -action

- (void)isKefu:(UIViewController*)controller actionBlock:(void(^)())block{
 
    [self setImage:XYImage(@"btn_xiaoneng_normal@2x") forState:UIControlStateNormal];
    [self setImage:XYImage(@"btn_xiaoneng_press@2x") forState:UIControlStateHighlighted];
    
    
     __block UIViewController *blockVC = controller;
    
    [self addAcionWithLimitTime:.2 controlEvents:UIControlEventTouchUpInside usingBlock:^(id sender) {
        
        NTalkerChatViewController *chatViewController = [[NTalkerChatViewController alloc] init];
        chatViewController.appKey = @"BD7A2BE1-35A3-43C3-8593-C3BB2DFB0DB7";
        chatViewController.siteid = @"kf_9644";
        chatViewController.settingid = @"kf_9644_1444446822895";
        chatViewController.sellerID = @"";
        chatViewController.pushOrPresent = NO;
        chatViewController.userid =[NSString stringWithFormat:@"%d",[Public sharedPublic].userID];
        chatViewController.username = [Public sharedPublic].userName;
        chatViewController.userlevel = 0;//是否是vip用户  [必填]    0:非VIP  1:是VIP
        chatViewController.issingle =-1;
        chatViewController.titleName = @"万题库客服";//自定义聊窗title [必填]
        chatViewController.erpparam = @"我是erp";//erp参数  [必填] 如果没有此参数，直接传空字符串@""
        chatViewController.goodsID = @"";//商品ID：客服端显示商品信息用 [必填] 有则传，没有则传空字符串@""；
        chatViewController.goodsURL = @"";
        UINavigationController *navgationController = [[UINavigationController alloc] initWithRootViewController:chatViewController];
        [blockVC presentViewController:navgationController animated:YES completion:nil];
        [chatViewController release];
        [navgationController release];
        
        if (block) {
            block();
        }
    }];
    
}


#pragma mark -drag

-(void)setAdsorbEnable:(BOOL)adsorbEnable
{
    objc_setAssociatedObject(self, AdsorbEnableKey,@(adsorbEnable), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
 
}

-(BOOL)isAdsorbEnable
{
    return [objc_getAssociatedObject(self, AdsorbEnableKey) boolValue];
}
CGPoint beginPoint;

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.highlighted = YES;
    if (![objc_getAssociatedObject(self, AdsorbEnableKey) boolValue]) {
        return;
    }
    UITouch *touch = [touches anyObject];
    beginPoint = [touch locationInView:self];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.highlighted=NO;
    if (![objc_getAssociatedObject(self, AdsorbEnableKey) boolValue]) {
        return;
    }
    UITouch *touch = [touches anyObject];
    
    CGPoint nowPoint = [touch locationInView:self];
    
    float offsetX = nowPoint.x - beginPoint.x;
    float offsetY = nowPoint.y - beginPoint.y;
    self.center = CGPointMake(self.center.x + offsetX, self.center.y + offsetY);
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (self.highlighted) {
        [self sendActionsForControlEvents:UIControlEventTouchUpInside];
        self.highlighted = NO;
    }
    if (self.superview && [objc_getAssociatedObject(self,AdsorbEnableKey) boolValue] ) {
        float marginLeft = self.frame.origin.x;
        float marginRight = self.superview.frame.size.width - self.frame.origin.x - self.frame.size.width;
        float marginTop = self.frame.origin.y;
        float marginBottom = self.superview.frame.size.height - self.frame.origin.y - self.frame.size.height;
        
        
        [UIView animateWithDuration:0.125 animations:^(void){
            if (marginTop<60) {
                self.frame = CGRectMake(marginLeft<marginRight?marginLeft<PADDING?PADDING:self.frame.origin.x:marginRight<PADDING?self.superview.frame.size.width -self.frame.size.width-PADDING:self.frame.origin.x,
                                        PADDING,
                                        self.frame.size.width,
                                        self.frame.size.height);
            }
            else if (marginBottom<60) {
                self.frame = CGRectMake(marginLeft<marginRight?marginLeft<PADDING?PADDING:self.frame.origin.x:marginRight<PADDING?self.superview.frame.size.width -self.frame.size.width-PADDING:self.frame.origin.x,
                                        self.superview.frame.size.height - self.frame.size.height-PADDING,
                                        self.frame.size.width,
                                        self.frame.size.height);
                
            }
            else {
                self.frame = CGRectMake(marginLeft<marginRight?PADDING:self.superview.frame.size.width - self.frame.size.width-PADDING,
                                        self.frame.origin.y,
                                        self.frame.size.width,
                                        self.frame.size.height);
            }
        }];
        
        
    }
    
    
}


@end
